package com.zhss.massivedata.order.service;

import com.zhss.massivedata.order.constant.OrderStatus;
import com.zhss.massivedata.order.domain.OrderInfo;
import com.zhss.massivedata.order.domain.OrderItem;
import com.zhss.massivedata.order.mapper.OrderMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public void create(OrderInfo orderInfo) throws Exception {
        orderMapper.insertOrderInfo(orderInfo);
        orderMapper.insertOrderItems(orderInfo.getOrderItems());

        // 遍历所有的订单条目，对每个条目都去执行库存锁定的操作
        for(OrderItem orderItem : orderInfo.getOrderItems()) {
            // 调用远程的库存系统，执行库存锁定
        }
    }

    @Transactional
    public void payed(long orderInfoId) throws Exception {
        orderMapper.updateOrderStatus(orderInfoId, OrderStatus.WAIT_FOR_DELIVERY);

        List<OrderItem> orderItems = orderMapper.selectItemsByOrderInfoId(orderInfoId);
        for(OrderItem orderItem : orderItems) {
            // 调用库存中心的接口，去进行库存的扣减
            System.out.println("对订单条目进行库存扣减：" + orderItem);
        }

        // 履约环节，一般可以做成异步的，因为他可以延迟一段时间
        // 调用履约中心的接口，调度中心+仓储中心，从距离用户最近的仓库里调度库存，去告诉
        // 仓储中心去进行发货，通知物流中心去进行拣货以及物流发货
    }

}
